package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.travelServer.entity.Permission;
import com.travel.travelServer.entity.Role;
import com.travel.travelServer.entity.RolePermission;
import com.travel.travelServer.entity.vo.RoleVO;
import com.travel.travelServer.mapper.PermissionMapper;
import com.travel.travelServer.mapper.RoleMapper;
import com.travel.travelServer.mapper.RolePermissionMapper;
import com.travel.travelServer.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2023-10-24 16:42:19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RolePermissionMapper rolePermissionDao;

    @Resource
    private PermissionMapper permissionDao;

    @Override
    public Page<RoleVO> pageByRole(Page<RoleVO> page, Map<String, Object> conditions) {
        Page<Role> rolePage = new Page<>(page.getCurrent(), page.getSize());
        QueryWrapper<Role> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like(Objects.nonNull(conditions.get("name")) && !conditions.get("name").equals("")
                ,"name", conditions.get("name"));
        userQueryWrapper.like(Objects.nonNull(conditions.get("code")) && !conditions.get("code").equals("")
                ,"code", conditions.get("code"));
        Page<Role> dbRole = super.page(rolePage, userQueryWrapper);
        List<RoleVO> roleVOS = new ArrayList<>();
        dbRole.getRecords().stream().forEach(role -> {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role,roleVO);
            QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
            rolePermissionQueryWrapper.lambda().eq(RolePermission::getRoleId,role.getId());
            List<RolePermission> rolePermissions = rolePermissionDao.selectList(rolePermissionQueryWrapper);
            if (rolePermissions.size() > 0){
                List<Long> permissionIds = rolePermissions.stream().map(
                        rolePermission -> rolePermission.getPermissionId()).collect(Collectors.toList());
                if (permissionIds.size() > 0){
                    List<Permission> permissions = permissionDao.selectBatchIds(permissionIds);
                    roleVO.setPermissionList(permissions);
                }
            }
            roleVOS.add(roleVO);
        });
        page.setTotal(rolePage.getTotal());
        return page.setRecords(roleVOS);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean setPermissions(List<Long> idList, Long id) {
        boolean result = false;
        QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",id);
        rolePermissionDao.delete(rolePermissionQueryWrapper);
        for (Long permission : idList) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(id).setPermissionId(permission);
            int row = rolePermissionDao.insert(rolePermission);
            if (row != 1){
                result = false;
            }else {
                result = true;
            }
        }
        return result;
    }
}

